<!-- 一键加载模板 index -->
<template>
	<view class="content" style="height: 100%;">
		<BaseNavBar :isnormal="true" title="一键加载模板" />
		<view class="body">
			<!-- cell -->
			<view class="template-list">
				<view class="template-item" :class="[idx==current?'current':'']" v-for="(obj,idx) in templateList"
					:key="idx" @click="selectTep(idx)">
					<image :src="obj.pic" mode="aspectFit" class="img"></image>
					<view class="content">
						<view class="title">{{obj.title}}</view>
						<view>{{obj.content}}</view>
					</view>
				</view>
			</view>
			<view class="btn-groups">
				<view class="btn" @click="chooseTep()">选取模板</view>
				<view class="btn" @click="creatTep()">新建模板</view>
				<view class="btn del" @click="delTep()">删除模板</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	import {
		getTemplateList,
		delTemplateInfor
	} from '../../api'
	export default {
		data() {
			return {
				templateList: [],
				current: -1
			}
		},
		mounted() {
			console.log('1111')
			this.getAllTemplate()
		},
		methods: {
			getAllTemplate() {
				getTemplateList('{}').then(res => {
					console.log('模版页===', res)
					if (res.status == 200) {
						this.templateList = res.data
						let picArr = []
						this.templateList.forEach((item) => {
							picArr = item.pictureArray.split(',')
							item.pic = picArr[0]
						})
					}
				})
			},
			selectTep(idx) {
				this.current = idx
			},
			chooseTep(){
				if(this.current < 0){
					uni.showToast({
						title: '请先选择模板',
						icon:'none',
					})
				} else {
					uni.navigateTo({
						url: "/pages/templateDetails/templateDetails?templateData=" + encodeURIComponent(JSON.stringify(this.templateList[this.current]))
					})
				}
				
			},
			creatTep(){
				uni.navigateTo({
					url: "/pages/templateDetails/templateDetails"
				})
			},
			delTep(){
				if(this.current<0){
					uni.showToast({
						title: '请先选择模板',
						icon:'none',
					})
					return
				}
				let userInfo = uni.getStorageSync('userInfo')
				let data = {
					id: userInfo.id,
				}
				// 删除模板
				delTemplateInfor(data,this.templateList[this.current].id).then(res => {
					console.log('一键加载模板页===', res)
					if (res.status == 200) {
						uni.showToast({
							title: '删除成功',
						})
						setTimeout(()=>{
							this.getAllTemplate()
						},500)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.template-list {
		padding: 46px 28px 100px;
		.template-item {
			display: flex;
			padding: 15px 19px;
			border: 1px solid #DFDFDF;
			border-radius: 8px;
			margin-bottom: 10px;
			font-size: 12px;
			color: #666;
			line-height: 16px;
			.img {
				width: 100px;
				height: 100px;
				border-radius: 3px;
				margin-right: 26px;
			}
			.content {
				margin-left: auto;
				text-align: justify;
				.title {
					font-size: 14px;
					color: #000;
					margin-bottom: 8px;
					font-weight: 700;
				}
			}
		}
	}

	.template-item.current {
		background: #EFF1F2;
		border-color: #2456E0;
		.content .title {
			color: #2456E0;
		}
	}

	.btn-groups{
		width: 100%;
		overflow: hidden;
		padding: 20px 0;
		background: #fff;
		display: flex;
		justify-content: space-around;
		box-shadow: 0 2px 6px 1px #0A19421A;
		position: fixed;
		left: 0;
		bottom: 0;
		.btn{
			padding: 0 20px;
			height: 40px;
			line-height: 40px;
			font-size: 14px;
			color: #fff;
			text-align: center;
			border-radius: 30px;
			box-shadow: 0 2px 6px 1px #0A19421A;
			background-color: #2456E0;
			&.del{
				background-color: #f00;
			}
		}
	}
	
</style>